package com.example.zhengmingli

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity

class AddStudentActivity : AppCompatActivity() {
    private lateinit var idEditText: EditText
    private lateinit var nameEditText: EditText
    private lateinit var genderSpinner: Spinner
    private lateinit var classNameEditText: EditText
    private lateinit var confirmButton: Button
    private lateinit var cancelButton: Button



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_student)

        idEditText = findViewById(R.id.student_id)
        nameEditText = findViewById(R.id.student_name)
        genderSpinner = findViewById(R.id.gender_spinner)
        classNameEditText = findViewById(R.id.class_name)
        confirmButton = findViewById(R.id.confirm_button)
        cancelButton = findViewById(R.id.cancel_button)

        confirmButton.setOnClickListener {
            val student = Student(
                idEditText.text.toString(),
                nameEditText.text.toString(),
                genderSpinner.selectedItem.toString(),
                classNameEditText.text.toString()
            )
            val intent = Intent()
            intent.putExtra("student", student)
            setResult(Activity.RESULT_OK, intent)
            finish()

        }

        val genderSpinner: Spinner = findViewById(R.id.gender_spinner)
        ArrayAdapter.createFromResource(
            this,
            R.array.gender_options,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            // Apply the adapter to the spinner
            genderSpinner.adapter = adapter
        }

        cancelButton.setOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            finish()
        }
    }
}
